<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/30 0030
 * Time: 16:46
 */

namespace app\api\model;


class Product extends BaseModel
{
    protected $hidden = ['delete_time','category_id','from','create_time','update_time','img_id','pivot'];
    public function getMainImgUrlAttr($value,$data){
        return $this->prefixImgUrl($value,$data);
    }
    public function imgs(){
        return $this->hasMany('ProductImage','product_id','id');
    }
    public function properties(){
        return $this->hasMany('ProductProperty','product_id','id');
    }
    public static function getMostRecent($count){
        $product = self::limit($count)
            ->order('create_time desc')
            ->select();
        return $product;
    }
    public static function getProductsByCategoryID($categoryID){
        $products = self::where('category_id','=',$categoryID)
            ->select();
        return $products;
    }
    public static function getProductDetail($id){
//        Query
        $products = self::with([
            'imgs' => function($query){
                        $query->with(['imgUrl'])
                            ->order('order','asc');
                    }
            ])
            ->with(['properties'])
            ->find($id);
        return $products;
    }
}